<template>
    <div v-show="isShow" id="user-set">
        <top-nav :title="title" />
        <top-option :optionSet="optionSet" @chooseOpt="chooseOpt" />
        <div id="all-information">
            <Account 
            v-show="optionSet.active === 'account'" 
            :accountData="data.account"
            @imgLoad="toImgLoad"
            @saveSignature="saveSignature"
            />
            <Safety 
            v-show="optionSet.active === 'safety'" 
            :safetyData="data.safety"
            @imgLoad="toImgLoad"
            />
            <Business 
            ref="business"
            v-show="optionSet.active === 'business'" 
            :businessData="data.business"
            @imgLoad="toImgLoad"
            />
        </div>
        <img-up-load ref="imgLoad" @imgUpLoadSuccess="imgUpLoadSuccess"/>
    </div>
</template>

<script>
// 自定义组件
import TopNav from '_components/project/common/TopNav'
import Account from '_components/project/SetUser/Account/Account'
import Safety from '_components/project/SetUser/Safety/Safety'
import Business from '_components/project/SetUser/Business/Business'
import TopOption from '_components/project/SetUser/TopOption'
import ImgUpLoad from '_components/project/common/ImgUpLoad'

// 自定义方法
import { checkDataEqualVal } from "_commonFn/check"
import { defaultShare } from "_commonFn/share"
import { getUserSet, changHeadImg } from "_network/request"
import { failTextVantToa, loadingVantToa, clearVantToa } from "_projectFn/prompt"
import { isLoginChose, } from '_projectFn/login'

export default {
    name: 'SetUser',
    data() {
        return {
            title: '账号信息/关键设置/业务名片',
            isShow: false,
            optionSet: {
                option: {
                    account: {
                        name: '账号信息',
                        id: 'account',
                        isDanger: false,
                    },
                    safety: {
                        name: '关键设置',
                        id: 'safety',
                        isDanger: false,
                    },
                    business: {
                        name: '业务名片',
                        id: 'business',
                        isDanger: false,
                    },
                },
                active: 'account'
            },
            data: {
                account: {
                    userId: null, // string	用户编号
                    nickName: null, // string	用户昵称
                    sex: null, // int	性别(1:男，0：女，-1：未知)
                    headImg: null, // string	头像
                    userLevel: null, // int	用户级别(0：普通用户,1:高级用户,2:资深用户,3:超级用户)
                    signature: null, // string	用户签名
                    regTime: null, // long	注册时间
                },
                safety: {
                    pwd: null, // 登录密码(1:已设置,0:未设置)
                    payPwd: null, // 支付密码(1:已设置,0:未设置)
                    email: null, // 电子邮箱(1:已设置,0:未设置)
                    emailNumber: null, // 电子邮箱地址
                    phone: null, // 手机(1:已设置,0:未设置)
                    phoneNumber: null, // 手机号码
                    subscribe: null, // 关注公众号(1:已关注,0:未关注)
                    firm: null, // 关注企业(1:已设置,0:未设置)
                    firmCount: null, // 企业关注数量
                    trade: null, // 关注行业(1:已设置,0:未设置)
                    tradeCount: null, // 行业关注数量
                    zh: null, // 关注展会(1:已设置,0:未设置)
                    zhCount: null, // 展会关注数量
                    goods: null, // 关注产品(1:已设置,0:未设置)
                    goodsCount: null, // 产品关注数量
                    bindWechat: null, // 绑定微信(1:已绑定,0:未绑定)
                    isDanger: false,
                    wechatNo: null,
                },
                business: {
                    sex: null, // int	性别:1:男,2:女
                    userWx: null, // string	我的微信
                    wxScan: null, // string	微信二维码请求地址
                    description: null, // string	微信备注
                    areaCode: null, // string	所在地区编号
                    areaName: null, // string	所在地区
                    supply: null, // string	我的供应
                    supplyGoods: null, // string	供应产品编号,以,号相隔
                    supplyGoodsName: null, // string	供应产品名称,以,号相隔
                    demand: null, // string	我的需求
                    demandGoods: null, // string	需求产品编号,以,号相隔
                    demandGoodsName: null, // string	需求产品名称,以,号相隔
                    findWho: null, // string	我要找谁
                    myHelp: null, // string	我能帮助
                    yourSelf: null, // string	介始自己
                    comCode: null, // string	所在企业编号
                    company: null, // string	所在企业
                    cardA: null, // string	名片正面URL地址
                    cardB: null, // string	名片反面URL地址
                    isPartner: null, // int	是否选择了我要找业务互助合作伙伴
                    isDanger: false,
                    isWrited: false,
                },
            },
            imgLoadData: {
                url: '',
                type: ''
            }
        }
    },
    methods: {
        // 保存签名
        saveSignature(str) {
            this.data.account.signature = str
        },
        // 保存用户主页信息
        setUserInfo(data) {
            // 账号信息
            this.data.account.userId = data.UserInfo.UserId // string	用户编号
            this.data.account.nickName = data.UserInfo.NickName // string	用户昵称
            this.data.account.sex = data.UserInfo.Sex // int	性别(1:男，0：女，-1：未知)
            this.data.account.headImg = data.UserInfo.HeadImg // string	头像
            this.data.account.userLevel = data.UserInfo.UserLevel // int	用户级别(0：普通用户,1:高级用户,2:资深用户,3:超级用户)
            this.data.account.signature = data.UserInfo.Signature // string	用户签名
            this.data.account.regTime = data.UserInfo.RegTime // long	注册时间

            // 关键设置
            this.data.safety.pwd = data.UserSetting.Pwd // 登录密码(1:已设置,0:未设置)
            this.data.safety.payPwd = data.UserSetting.PayPwd // 支付密码(1:已设置,0:未设置)
            this.data.safety.email = data.UserSetting.Email // 电子邮箱(1:已设置,0:未设置)
            this.data.safety.emailNumber = data.UserSetting.EmailNumber // 电子邮箱地址
            this.data.safety.phone = data.UserSetting.Phone // 手机(1:已设置,0:未设置)
            this.data.safety.phoneNumber = data.UserSetting.PhoneNumber // 手机号码
            this.data.safety.subscribe = data.UserSetting.Subscribe // 关注公众号(1:已关注,0:未关注)
            this.data.safety.firm = data.UserSetting.Firm // 关注企业(1:已设置,0:未设置)
            this.data.safety.firmCount = data.UserSetting.FirmCount // 企业关注数量
            this.data.safety.trade = data.UserSetting.Trade // 关注行业(1:已设置,0:未设置)
            this.data.safety.tradeCount = data.UserSetting.TradeCount // 行业关注数量
            this.data.safety.zh = data.UserSetting.Zh // 关注展会(1:已设置,0:未设置)
            this.data.safety.zhCount = data.UserSetting.ZhCount // 展会关注数量
            this.data.safety.goods = data.UserSetting.Goods // 关注产品(1:已设置,0:未设置)
            this.data.safety.goodsCount = data.UserSetting.GoodsCount // 产品关注数量
            this.data.safety.bindWechat = data.UserSetting.BindWechat // 绑定微信(1:已绑定,0:未绑定)

            // 业务名片
            this.data.business.sex = data.UserBasic.Sex // int	性别:1:男,2:女
            this.data.business.userWx = data.UserBasic.UserWx // string	我的微信
            this.data.business.wxScan = data.UserBasic.WxScan // string	微信二维码请求地址
            this.data.business.description = data.UserBasic.Description // string	微信备注
            this.data.business.areaCode = data.UserBasic.AreaCode // string	所在地区编号
            this.data.business.areaName = data.UserBasic.AreaName // string	所在地区
            this.data.business.supply = data.UserBasic.Supply // string	我的供应
            this.data.business.supplyGoods = data.UserBasic.SupplyGoods // string	供应产品编号,以,号相隔
            this.data.business.supplyGoodsName = data.UserBasic.SupplyGoodsName // string	供应产品名称,以,号相隔
            this.data.business.demand = data.UserBasic.Demand // string	我的需求
            this.data.business.demandGoods = data.UserBasic.DemandGoods // string	需求产品编号,以,号相隔
            this.data.business.demandGoodsName = data.UserBasic.DemandGoodsName // string	需求产品名称,以,号相隔
            this.data.business.findWho = data.UserBasic.FindWho // string	我要找谁
            this.data.business.myHelp = data.UserBasic.MyHelp // string	我能帮助
            this.data.business.yourSelf = data.UserBasic.YourSelf // string	介始自己
            this.data.business.comCode = data.UserBasic.ComCode // string	所在企业编号
            this.data.business.company = data.UserBasic.Company // string	所在企业
            this.data.business.cardA = data.UserBasic.CardA // string	名片正面URL地址
            this.data.business.cardB = data.UserBasic.CardB // string	名片反面URL地址
            this.data.business.isPartner = data.UserBasic.IsPartner // int	是否选择了我要找业务互助合作伙伴
            this.data.business.isWrited = Boolean(data.UserBasic.Sex || data.UserBasic.UserWx || data.UserBasic.WxScan || data.UserBasic.AreaCode)
        },
        // 获取页面所需信息
        getData() {
            this.isShow = false
            if (isLoginChose()) {
                this.getMyInfo()
            }
        },
        // 获取我的所有信息
        getMyInfo() {
            loadingVantToa({})
            getUserSet()
            .then(res => {
                console.log("获取用户的所有信息 res：", res)
                if (res.errcode == 0) {
                    this.isShow = true
                    this.setUserInfo(res.data) // 保存用户主页信息
                    this.setIsDanger(res.data)
                    // this.$refs.business.initData()
                    clearVantToa()
                } else {
                    failTextVantToa(res.message)
                }
            }, res => {
                console.log("fail: res", res)
                if (res.errcode != 2000) {
                    failTextVantToa("服务器正在修整，请稍后")
                }
            })
            .catch(res => {
                console.log("catch：res", res)
                failTextVantToa("服务器正在修整，请稍后")
            })
        },
        // 设置是否显示危险图标
        setIsDanger(data) {
            const security = !Boolean(Number(data.Security))
            const isWarit = !Boolean(Number(data.IsWarit))
            this.optionSet.option.safety.isDanger = security
            this.optionSet.option.business.isDanger = isWarit
            this.data.safety.isDanger = security
            this.data.business.isDanger = isWarit
            this.data.safety.wechatNo = data.UserBasic.UserWx // 绑定微信(1:已绑定,0:未绑定)
        },
        // 点击选择顶部选项
        chooseOpt(id) {
            this.optionSet.active = id
        },
        // 上传图片
        toImgLoad(type) {
            if (type) {
                this.imgLoadData.type = type
            }
            this.$refs.imgLoad.choose()
        },
        // 图片上传成功
        imgUpLoadSuccess(url) {
            // console.log('url', url)
            if (this.imgLoadData.type === 'user') {
                loadingVantToa({})
                changHeadImg(url)
                .then(res => {
                    console.log("修改头像 res：", res)
                    if (res.errcode == 1000) {
                        clearVantToa()
                        this.data.account.headImg = url
                    } else {
                        failTextVantToa(res.message)
                    }
                }, res => {
                    console.log("fail: res", res)
                    if (res.errcode != 2000) {
                        failTextVantToa("服务器正在修整，请稍后")
                    }
                })
                .catch(res => {
                    console.log("catch：res", res)
                    failTextVantToa("服务器正在修整，请稍后")
                })
            }
        }
    },
    activated() {
        this.getData()
        const that = this
        if (that.$store.getters.isWechatBro) {
			checkDataEqualVal({
				data: that.$store.getters,
				key: 'getWechatStatus',
				val: ['success', 'fail'],
				callback(val) {
					if (val === 'success') {
                        defaultShare({
                            title: this.title,
                        })
                    }
				}
			})
        }
        if (this.$route.query.option) {
            this.optionSet.active = this.$route.query.option
        }
    },
    components: {
        TopNav,
        Account,
        Safety,
        Business,
        TopOption,
        ImgUpLoad,
    }
}
</script>

<style lang="scss" scoped>

</style>
<style lang="scss">
#user-set {
    height: 100%;
    background: #efefef;

    .color-999 {
        color: #999999;
    }
}
</style>